In this demonstration,
you will learn about the product
and order service. Start with the product
module.js file, import mongoose, and
create the product schema. The product schema will have the product ID with
type as string, and require value as true. Similarly, add the product
name, and product details. Now create the product model, and pass the parameter
as the model name, and the schema name. Export the module, next, go to the DAO layer. First you will require
the DB config file, create an A sync function. Save product with the
product, and done parameters. Call the product.Save method, that will return
the data that needs to be stored in the
data variables. Return the callback with the undefined, and data parameters. Create the get product by ID method with three parameters, product, product ID, and done. The method will call
the find by ID method, and pass the product ID, export both the functionalities. Similarly in the service class, import the DAO layer, create a safe product
method with two parameters, product, and done. Call the DAO method, safe product, and
pass the parameter. Create, get product by ID
method with three parameters, call the DAO, get product by ID method
passing the three parameters, export the functionalities so
you get the service layer. In the controller layer, import the service layer, create the safe product method, and call the service method. Create get product by ID
with three parameters. Call the service method
get product by ID, and pass the three parameters, export all the functionalities. Now you will work with
the product router class. Import the express module, and create the router. Import product controller, and the product model. Also, create the post
method router dot post, passing the route parameter, and the callback, and
try, and catch blocks. Inside the try-block,
create the product object, retrieve the data from
the request body, and pass it inside
the product object. Call the controller
save product method, passing the new product,
and the callback. If the controller
returns the error, then the status will be
400 with a message as, error while saving product, print the error as well. Inside the else block, send statuses 200,
and the result. Inside the catch block, display status as 500 with the error message as
internal server error. You have to print
the error as well. So in the router you will create one more method, router dot get, and the parameter
is ID with request, and response as a
callback method. Retrieve the product ID from
the request parameters. Now, call the controller layer, get product by ID method, pass the product
ID, and call back. If the controller
returns the error, the status will be 400 with the message, error
getting product. Else the status will be
200, and the result. Inside the catch block, you can have the status as 500 with a message
internal server error, print the error object, export this module router. Now inside the index.js, import the express
end product router. Use the express dot.JSON, and product router, our application is
listening on port 3000, run the service. First, go inside the product
service, add node index.js. You will get listening, and connected on the screen, so the service is
listening on port 3000, and it's connected
to the database. Now, open the postman
Inside the body, add the product name,
say Samsung mobile. Then add the product ID, say one, two, three, and the product details, say electronics, send the
request so you get an ID. With this, you are done with the product layer. Thank you.